(function (line) {
    line.define('line.client.ControllerResolver', null, {
        singleton:true,
        properties:{
            path:{
                defaultValue:'controllers'
            },
            namespace:{
                defaultValue:'site.controllers'
            }
        },
        methods:{
            init:function () {
                this._controllers = {};
            },
            resolve:function (name, callback) {
                var type = this.getProperty('namespace') + '.' + name;
                var controllers = this._controllers;
                var controller = controllers[type];

                if (controller) {
                    callback(controller);
                }
                else {
                    var path = this.getProperty('path') + '/' + name;
                    var clazz;
                    require([path], function () {
                        clazz = line.getClass(type);
                        if (clazz) {
                            controller = controllers[type] = new clazz;
                            callback(controller);
                        }
                        else {
                            throw new Error('Cannot resolve controller ' + type);
                        }
                    });
                }
            }
        }
    });
})(line);